<template>
	<div class="app-container">
		<el-form class="queryForms" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="70px">
			<el-form-item label="产品名称" prop="productName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productName" placeholder="请输入产品名称"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="返修任务单" prop="repairNumber" label-width="90px">
				<el-input maxlength="20" show-word-limit v-model="queryParams.repairNumber" placeholder="请输入返修任务单"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品编号" prop="productNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productNumber" placeholder="请输入产品编号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="3B号" prop="bNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.bNumber" placeholder="请输入3B号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="故障核实情况" prop="malfunctionStatus" label-width="100px">
				<el-select v-model="queryParams.malfunctionStatus" placeholder="请选择">
					<el-option label="无故" value="0"></el-option>
					<el-option label="有故" value="1"></el-option>
					<el-option label="待核实" value="2"></el-option>
					<el-option label="正常" value="3"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="产品型号" prop="productModel">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productModel" placeholder="请输入产品型号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="工段" prop="workshopSectionId" label-width="70px">
				<el-select v-model="queryParams.workshopSectionId" placeholder="请选择">
					<el-option v-for="item in options" :key="item.id" :label="item.workshopSectionName"
						:value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<right-toolbar @changeTableData="changeTableData" menuNames="taskFilling72" :showSearch.sync="showSearch"
				@queryTable="getList" :columns.sync="columns" perm="erp:taskFilling:list"></right-toolbar>
		</el-row>

		<div class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="repairProductProcessList"
				@selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="返修任务单" prop="repairNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[0].visible" />
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[1].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[2].visible" />
				<el-table-column label="产品编号" prop="productNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[3].visible" />
				<el-table-column label="3B号" prop="bNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[4].visible" />
				<el-table-column label="工段" prop="workshopSectionName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[9].visible" />
				<el-table-column label="维修类别" prop="maintenanceCategoryName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[5].visible" />
				<!-- <el-table-column label="故障原因" prop="failureCause" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" /> -->
				<el-table-column label="故障模式" prop="failureMode" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[6].visible" />
				<el-table-column label="故障核实情况" align="center" prop="malfunctionStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[7].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.malfunctionStatus == '0'">无故</div>
						<div v-if="scope.row.malfunctionStatus == '1'">有故</div>
						<div v-if="scope.row.malfunctionStatus == '2'">待核实</div>
						<div v-if="scope.row.malfunctionStatus == '3'">正常</div>
					</template>
				</el-table-column>
				<el-table-column label="工艺通知单编号" prop="processNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[8].visible" />
				<el-table-column align="center" label="填写状态" prop="technologyStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[10].visible">
					<template slot-scope="scope">
						<el-tag type="warning" v-if="scope.row.technologyStatus == 0">进行中</el-tag>
						<el-tag type="success" v-if="scope.row.technologyStatus == 1">已完成</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="200"
					align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
							v-hasPermi="['erp:taskFilling:enter']">任务填报</el-button>
						<el-dropdown size="mini" @command="command => handleCommand(command, scope.row)"
							v-hasPermi="['system:help:check1', 'system:help:military', 'system:help:pack', 'system:help:quality', 'system:help:truck', 'system:help:editWork', 'system:help:viewWork']">
							<el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
							<el-dropdown-menu slot="dropdown">
								<el-dropdown-item command="handleHelp4"
									v-hasPermi="['system:help:quality']">协助质检</el-dropdown-item>
								<el-dropdown-item command="handleHelp2"
									v-hasPermi="['system:help:military']">协助军检</el-dropdown-item>
								<el-dropdown-item command="handleHelp1"
									v-hasPermi="['system:help:check1']">协助检查</el-dropdown-item>
								<el-dropdown-item command="handleHelp3"
									v-hasPermi="['system:help:pack']">协助包装</el-dropdown-item>
								<el-dropdown-item command="handleHelp5"
									v-hasPermi="['system:help:truck']">协助装车</el-dropdown-item>
								<el-dropdown-item command="handleHelp6"
									v-hasPermi="['system:help:editWork']">修改协助工时</el-dropdown-item>
								<el-dropdown-item command="handleHelp7"
									v-hasPermi="['system:help:viewWork']">查看协助工时</el-dropdown-item>
							</el-dropdown-menu>
						</el-dropdown>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<pagination menuNames="Pagination75" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />
		<!-- 添加或修改返修单产品工序列对话框 -->
		<el-dialog title="详情" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="90px">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="工艺流名称" prop="productTechnologyName">
							<el-input v-model="form.productTechnologyName" placeholder="请输入工艺流名称" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="工序编码" prop="processCode">
							<el-input v-model="form.processCode" placeholder="请输入工序编码" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="工序名称" prop="processName">
							<el-input v-model="form.processName" placeholder="请输入工序名称" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="工时" prop="processTime">
							<el-input v-model="form.processTime" placeholder="请输入工时" disabled />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="完成人名称" prop="accomplishUserName">
							<el-input v-model="form.accomplishUserName" placeholder="请输入完成人名称" disabled />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-dialog>

		<el-dialog title="协助" :visible.sync="openHelp" width="700px" append-to-body v-dialogDrag>
			<el-form ref="formHelp" :model="formHelp" :rules="rulesHelp" label-width="90px">
				<div v-if="statusHelp == 1 || statusHelp == 6 || statusHelp == 7">
					<el-form-item label="检查人员" prop="appearanceInspectionUserId">
						<el-select v-model="formHelp.appearanceInspectionUserId" :disabled="disableHelp" clearable>
							<el-option v-for="item in wordData" :key="item.userId" :label="item.nickName"
								:value="item.userId"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="检查时间" prop="appearanceInspectionTime">
						<el-date-picker v-model="formHelp.appearanceInspectionTime" clearable :disabled="disableHelp"
							format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间">
						</el-date-picker>
					</el-form-item>
				</div>
				<div v-if="statusHelp == 2 || statusHelp == 6 || statusHelp == 7">
					<el-form-item label="军检人员" prop="militaryUserId">
						<el-select v-model="formHelp.militaryUserId" :disabled="disableHelp" clearable>
							<el-option v-for="item in wordData" :key="item.userId" :label="item.nickName"
								:value="item.userId"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="军检时间" prop="militaryTime">
						<el-date-picker v-model="formHelp.militaryTime" clearable :disabled="disableHelp"
							format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间">
						</el-date-picker>
					</el-form-item>
				</div>
				<div v-if="statusHelp == 3 || statusHelp == 6 || statusHelp == 7">
					<el-form-item label="包装人员" prop="packUserId">
						<el-select v-model="formHelp.packUserId" :disabled="disableHelp" clearable>
							<el-option v-for="item in wordData" :key="item.userId" :label="item.nickName"
								:value="item.userId"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="包装时间" prop="packTime">
						<el-date-picker v-model="formHelp.packTime" clearable :disabled="disableHelp"
							format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间">
						</el-date-picker>
					</el-form-item>
				</div>
				<div v-if="statusHelp == 4 || statusHelp == 6 || statusHelp == 7">
					<el-form-item label="质检人员" prop="qualityUserId">
						<el-select v-model="formHelp.qualityUserId" :disabled="disableHelp" clearable>
							<el-option v-for="item in wordData" :key="item.userId" :label="item.nickName"
								:value="item.userId"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="质检时间" prop="qualityTime">
						<el-date-picker v-model="formHelp.qualityTime" clearable :disabled="disableHelp"
							format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间">
						</el-date-picker>
					</el-form-item>
				</div>
				<div v-if="statusHelp == 5 || statusHelp == 6 || statusHelp == 7">
					<el-form-item label="装车人员" prop="truckUserId">
						<el-select v-model="formHelp.truckUserId" :disabled="disableHelp" clearable>
							<el-option v-for="item in wordData" :key="item.userId" :label="item.nickName"
								:value="item.userId"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="装车时间" prop="truckTime">
						<el-date-picker v-model="formHelp.truckTime" clearable :disabled="disableHelp"
							format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间">
						</el-date-picker>
					</el-form-item>
				</div>
			</el-form>
			<div slot="footer" class="dialog-footer" v-if="!disableHelp">
				<el-button type="primary" @click="submitHelp">确 定</el-button>
				<el-button @click="cancelHelp">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getcolumn } from "@/api/selectList"
import { submitedit1, submitproductWorkpoints, getworkList, getrepairProduct, productList, getRepairProductProcess, delRepairProductProcess, getOption } from "@/api/taskFilling/repairProductProcess"
import { mapGetters } from "vuex"

import searchStateMixin from '@/layout/mixin/searchStateMixin.js'
export default {
	name: "RepairProductProcess",
	mixins: [searchStateMixin],
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			disableHelp: false,
			// 表单校验
			rulesHelp: {
				// appearanceInspectionUserId: [{ required: true, message: "检查人员不能为空", trigger: "blur" }],
				// appearanceInspectionTime: [{ required: true, message: "检查时间不能为空", trigger: "blur" }],
				// militaryTime: [{ required: true, message: "军检时间不能为空", trigger: "blur" }],
				// militaryUserId: [{ required: true, message: "军检人员不能为空", trigger: "blur" }],
				// packTime: [{ required: true, message: "包装时间不能为空", trigger: "blur" }],
				// packUserId: [{ required: true, message: "包装人员不能为空", trigger: "blur" }],
				// qualityTime: [{ required: true, message: "质检时间不能为空", trigger: "blur" }],
				// qualityUserId: [{ required: true, message: "质检人员不能为空", trigger: "blur" }],
				// truckTime: [{ required: true, message: "装车时间不能为空", trigger: "blur" }],
				// truckUserId: [{ required: true, message: "装车人员不能为空", trigger: "blur" }]
			},
			wordData: [],
			openHelp: false,
			formHelp: {},
			statusHelp: 1,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			processStatusList: [
				{
					id: "0",
					name: "未完工"
				},
				{
					id: "1",
					name: "已完工"
				},
				{
					id: "2",
					name: "质检通过"
				},
				{
					id: "3",
					name: "质检不通过"
				}
			],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 返修单产品工序列表格数据
			repairProductProcessList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				repairProductId: null,
				productProcessId: null,
				productTechnologyId: null,
				productTechnologyName: null,
				processCode: null,
				processName: null,
				productName: null,
				productModel: null,
				processTime: null,
				content: null,
				processStatus: null,
				accomplishUserId: null,
				accomplishUserName: null,
				repairNumber: null,
				productNumber: null,
				bNumber: null,
				malfunctionStatus: null,
				workshopSectionId: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `返修任务单`, visible: true },
				{ key: 1, label: `产品名称`, visible: true },
				{ key: 2, label: `产品型号`, visible: true },
				{ key: 3, label: `产品编号`, visible: true },
				{ key: 4, label: `3B号`, visible: true },
				{ key: 5, label: `维修类别`, visible: true },
				{ key: 6, label: `故障模式`, visible: true },
				{ key: 7, label: `故障核实情况`, visible: true },
				{ key: 8, label: `工艺通知单编号`, visible: true },
				{ key: 9, label: `工段`, visible: true },
				{ key: 10, label: `填写状态`, visible: true }
			],
			options: [],
			isActive: true // 标记组件是否活跃
		}
	},
	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	watch: {
		"$route.query": {
			handler(newQuery, oldQuery) {
				// if (!this.isActive) return // 如果组件已销毁/离开，不执行逻辑
				if (newQuery.queryData && Object.keys(newQuery.queryData).length != 0) {
					this.queryParams = { ...this.queryParams, ...JSON.parse(newQuery.queryData) }
					// this.getSize()
				}
				this.getSize()
			},
			immediate: true, // 立即执行一次
			deep: true // 深度监听
		}
	},
	activated() {
		// this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	beforeDestroy() {
		this.isActive = false // 标记组件已销
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination75" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 更多操作触发
		handleCommand(command, row) {
			switch (command) {
				case "handleHelp1":
					this.handleHelp(row, 1)
					break
				case "handleHelp2":
					this.handleHelp(row, 2)
					break
				case "handleHelp3":
					this.handleHelp(row, 3)
					break
				case "handleHelp4":
					this.handleHelp(row, 4)
					break
				case "handleHelp5":
					this.handleHelp(row, 5)
					break
				case "handleHelp6":
					this.handleHelp(row, 6)
					break
				case "handleHelp7":
					this.handleHelp(row, 7)
					break
				default:
					break
			}
		},
		resetHelp() {
			this.formHelp = {
				appearanceInspectionName: undefined,
				appearanceInspectionTime: undefined,
				appearanceInspectionUserId: undefined,
				militaryName: undefined,
				militaryTime: undefined,
				militaryUserId: undefined,
				packName: undefined,
				packTime: undefined,
				packUserId: undefined,
				qualityName: undefined,
				qualityTime: undefined,
				qualityUserId: undefined,
				repairProductId: undefined,
				truckTime: undefined,
				truckUserId: undefined,
				truckTime: undefined
			}
		},
		cancelHelp() {
			this.resetHelp()
			this.openHelp = false
		},
		submitHelp() {
			this.$refs["formHelp"].validate(valid => {
				if (valid) {
					if (this.statusHelp != 6) {
						submitproductWorkpoints(this.formHelp).then(response => {
							this.$modal.msgSuccess("修改成功")
							localStorage.setItem("todoMessage", "taskFilling")
							this.openHelp = false
						})
					} else {
						submitedit1(this.formHelp).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.openHelp = false
						})
					}
				}
			})
		},
		handleHelp(row, status) {
			this.resetHelp()
			this.statusHelp = status
			if (status == 7) {
				this.disableHelp = true
			} else {
				this.disableHelp = false
			}
			this.workList(row.workshopSectionId)
			getrepairProduct(row.id).then(res => {
				this.formHelp = res.data
				this.openHelp = true
			})
		},
		workList(id) {
			getworkList(id).then(res => {
				this.wordData = res.rows
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		handleExport(row) {
			this.download(`/data/processWord/spring/${row.id}`, {}, `${row.processType == "1" ? "弹簧测力报告" : "探伤报告"}_${new Date().getTime()}.docx`)
		},
		handleDetail(row) {
			getRepairProductProcess(row.id).then(res => {
				this.form = res.data
				this.open = true
			})
		},
		/** 查询返修单产品工序列列表 */
		getList() {
			this.loading = true
			productList(this.queryParams).then(response => {
				this.repairProductProcessList = response.rows
				this.total = response.total
				this.loading = false
			})
			this.getOption()
		},
		async getOption() {
			try {
				const res = await getOption()
				this.options = res.rows
			} catch (error) {
				this.$modal.msgError("获取工段数据失败")
				this.options = []
			}
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				repairProductId: null,
				productProcessId: null,
				productTechnologyId: null,
				productTechnologyName: null,
				processCode: null,
				processName: null,
				productName: null,
				productModel: null,
				processTime: null,
				content: null,
				processStatus: null,
				accomplishUserId: null,
				accomplishUserName: null,
				remark: null,
				repairNumber: null,
				updateTime: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.saveSearchState()
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.queryParams.productModel = undefined
			this.queryParams.productNumber = undefined
			this.clearSearchCache()
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加返修单产品工序列"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			// status   1修改   2 填写
			this.$router.push({
				path: "/taskFilling/taskFill/menuList/index",
				query: {
          queryData:JSON.stringify(this.queryParams),
					fillingId: row.id,
					productName: row.productName,
					productModel: row.productModel,
					productNumber: row.productNumber,
				}
			})
		},
		/** 提交按钮 */
		submitForm() { },
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除返修单产品工序数据项？")
				.then(function () {
					return delRepairProductProcess(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => { })
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 100px !important;
}
</style>
